package com.meanbean.studytime.controls;

import java.io.IOException;

import com.meanbean.studytime.StudyTimeMain;
import com.meanbean.studytime.StudyTimeStage;

import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

/**
 * A button which when open displays a TextField and when closed hides the TextField.
 * There is an answer String associated with each button instance.  If the TextField text
 * is equal to the answer String, then the button turns Green, otherwise it turns Red.  If there
 * is no string in the TextField, then the button turns Gray.
 * @author Derek Webb
 */
public class Button extends AnchorPane
{
	@FXML protected ImageView imageView;
	protected TextField textField;
	
	protected String answer;
	
	protected int xPosition;
	protected int yPosition;
	
	private boolean isOpen;
	
	/**
	 * Creates a StudyTimeButton object.
	 */
	public Button(int x, int y, String answer)
	{
		FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Button.fxml"));
		fxmlLoader.setRoot(this);
		fxmlLoader.setController(this);
		
		try
		{
			fxmlLoader.load();
			
			// Set the initial image color to gray
			imageView.setImage(new Image("com/meanbean/studytime/resources/GrayButton.png"));
			
			// Store the answer
			this.answer = answer;
			
			// Store the location for this button
			this.xPosition = x;
			this.yPosition = y;
			
			// Set the location for this button
			setLayoutX(x);
			setLayoutY(y);
			
			// Create the TextField
			textField = new TextField();
			textField.setLayoutX(this.getLayoutX() + 15);
			textField.setLayoutY(this.getLayoutY());
			textField.setVisible(true);
			textField.setPrefWidth(200);
			textField.addEventHandler(KeyEvent.KEY_PRESSED,
					new EventHandler<KeyEvent>()
					{
						public void handle(KeyEvent keyEvent)
						{
							// If the enter key was pressed, close the button
							if (keyEvent.getCode() == KeyCode.ENTER)
								close();
						}
					});
			
			// Close the button initially
			this.close();
		}
		catch (IOException e)
		{
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * @return A new StudyButton with the properties of this button.
	 */
	public StudyButton copyAsStudyButton()
	{
		return new StudyButton(this.xPosition, this.yPosition, this.answer);
	}
	
	/**
	 * @return A new EditButton with the properties of this button.
	 */
	public EditButton copyAsEditButton()
	{
		return new EditButton(this.xPosition, this.yPosition, this.answer);
	}
	
	/**
	 * @return The TextField associated with this button.
	 */
	public TextField getTextField()
	{
		return textField;
	}
	
	/**
	 * Either opens or closes depending upon the current state.  This will close the StudyTimeButton
	 * if it is open and it will open the StudyTimeButton if it is closed.
	 * @param event The MouseEvent.
	 */
	@FXML
	protected void toggle(MouseEvent event)
	{
		// If left mouse button, open or close the button
		if (event.getButton() == MouseButton.PRIMARY)
		{
			if (isOpen)
				close();
			else
				open();
		}
		// If right mouse button, ask the user if they want to delete the button (only in Edit mode)
		else if (event.getButton() == MouseButton.SECONDARY)
		{
			// Show the answer if in Study mode
			if (StudyTimeStage.getMode() == StudyTimeStage.Mode.Study)
			{
				// Open this button
				open();
				
				// Set the TextField text to the answer
				textField.setText(answer);
				
				// Select all of the text
				textField.selectAll();
			}
			// Ask the user if they want to delete this button if in Edit mode
			else if (StudyTimeStage.getMode() == StudyTimeStage.Mode.Edit)
			{
				DialogResult dialog = new DialogResult("Delete Button", "Are you sure you want to delete this button\n\"" + answer + "\"");
				dialog.initOwner(StudyTimeMain.studyTimeStage);
				dialog.showAndWait();
				
				// If the user clicked 'Yes', then remove the button.
				if (dialog.getResult() == DialogResult.Result.Yes)
					StudyTimeStage.getStudyControl().removeButton(this);
			}
		}
	}
	
	/**
	 * Opens the StudyTimeButton, showing the TextField.
	 */
	public void open()
	{
		// Show the TextField
		textField.setVisible(true);
		
		// Bring this Button and TextField to the front
		this.toFront();
		textField.toFront();
		
		// Request focus on the TextField
		textField.requestFocus();
		
		// Set flag to open
		isOpen = true;
	}
	
	/**
	 * Closes the StudyTimeButton, hiding the TextField from view.
	 */
	public void close()
	{
		// Hide the TextField
		textField.setVisible(false);
		
		// Bring this Button to the front
		this.toFront();
		
		// Request focus on this Button.
		// We do this because the ScrollPane was scrolling
		// up every time we would close a Button (for unknown reasons).
		// But, focusing on the Button when closing it makes the ScrollPane
		// stay still, so keep doing this!  :)
		this.requestFocus();
		
		// Set flag to close
		isOpen = false;
	}
	
	/**
	 * Saves the current answer.
	 */
	public void saveCurrentAnswer()
	{
		answer = textField.getText();
	}
	
	/**
	 * @return The answer for this button.
	 */
	public String getAnswer()
	{
		return answer;
	}
	
	/**
	 * @return The X position of this button.
	 */
	public int getX()
	{
		return xPosition;
	}
	
	/**
	 * @return The Y position of this button.
	 */
	public int getY()
	{
		return yPosition;
	}
}
